package analysis;

import common.AnalysisWare;
import common.Constants;
import common.DataAnalysis;
import redis.clients.jedis.Tuple;
import util.FileUtil;
import util.remote.RemoteConnectResult;
import util.remote.RemoteType;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by J.Chen on 2017/5/25.
 */

@AnalysisWare(confCode = "ACT_2017_04_RANK_TALENT", desc = "冲榜达人", remote = RemoteType.wan360)
public class RankTalentAnalysis extends DataAnalysis {
    public RankTalentAnalysis(RemoteConnectResult result, String confCode) {
        super(result, confCode);
    }

    @Override
    public void execute() {
        for (int type = 1; type < 4; type++) {
            FileUtil fileUtil = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), actConf.getConfCode(), actConf.getTimeStartStr(), "rank" + type));
            Set<Tuple> set = result.getRedisUtil().zrevrangeWithScores(getRankKey(type), 0, 49);
            for (Tuple t : set) {
                fileUtil.writeln("|", t.getElement(), "" + t.getScore());
            }
            fileUtil.close();
        }
        String sql = "select * from user_info__%d where lv_exp >= 30 ";
        int count = 0;
        FileUtil fileUtil = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), actConf.getConfCode(), actConf.getTimeStartStr(), "30级id"));
        for (int i = 0; i < 64; i++) {
            List<Map<String, String>> list = result.getMysqlUtil().selectList(String.format(sql, i));
            count += list.size();
            for (Map<String, String> m : list) {
                fileUtil.writeln("", m.get("user_id"));
            }


        }
        fileUtil.writeln("", "总的人数:" + count);
        fileUtil.close();


    }

    public String getRankKey(int type) {
        return String.join(":", "zAct", "open", "server", "act", "rank", "type", "" + type);
    }
}
